package com.amtf.datafile3.cfg.std;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.amtf.datafile3.DataException;
import com.amtf.datafile3.DataFieldDefinition;
import com.amtf.datafile3.DataRecord;
import com.amtf.datafile3.DataRecordParser;
import com.amtf.datafile3.DataRecordRender;
import com.amtf.datafile3.DataTableDefinition;
import com.amtf.datafile3.core.std.DataRecordImpl;

public abstract class StdDataTableDefinition extends StdDefinition implements DataTableDefinition {
	public static final String DEFAULT_COLNAME = "Column";

	protected List<DataFieldDefinition> fieldDefinitions = new ArrayList<DataFieldDefinition>();
	protected DataRecordParser parser = null;
	protected DataRecordRender render = null;

	// protected String function = "";

	public abstract DataTableDefinition clone();

	public StdDataTableDefinition(String name, DataRecordParser parser, DataRecordRender render) {
		super(name);
		this.render = render;
		this.parser = parser;
//		this.parser.setDefinition(this);
	}

	public void copyTo(StdDataTableDefinition newObj) {
		newObj.name = this.name;
		newObj.parser = this.parser.clone();
		newObj.render = this.render.clone();
		for (DataFieldDefinition def : fieldDefinitions) {
			newObj.fieldDefinitions.add((DataFieldDefinition) def.clone());
		}
	}

//	public DataTableDefinition clone() {
//		StdDataTableDefinition newObj = new StdDataTableDefinition(null, null, null);
//		copyTo(newObj);
//		return newObj;
//	}

	public DataFieldDefinition getField(int index) {
		// int cape = (index + 1) - fieldDefinition.size();
		// if (cape > 0) {
		// for (int i = 0; i < cape; i++) {
		// fieldDefinition.add(new T("Column" + (fieldDefinition.size() + 1), this));
		// }
		// }
		// if (index >= fieldDefinitions.size()) {
		// return null;
		// }

		return fieldDefinitions.get(index);
	}

	public DataFieldDefinition getField(String name) {
		for (DataFieldDefinition def : fieldDefinitions) {
			if (def.getName().equalsIgnoreCase(name)) {
				return def;
			}
		}

		return null;
	}

	// public DataFieldDefinition addField() {
	// return (DataFieldDefinition) addStringField(DEFAULT_COLNAME + (fieldDefinition.size() + 1));
	// }

	// public List addFields(int count) {
	// List newFieldDef = new ArrayList();
	// for (int i = 0; i < count; i++) {
	// newFieldDef.add(addField());
	// }
	//
	// return newFieldDef;
	// }

	// public DataRecord newDataRecordInstance() {
	// try {
	// return (ExcelDataRecord) dataRecordClass.getConstructors()[0].newInstance(this);
	// } catch (Throwable e) {
	// // e.printStackTrace();
	// }
	//
	// return null;
	// }

	public void addFields(Collection<DataFieldDefinition> all) {
		for (DataFieldDefinition dataFieldDefinition : all) {
			fieldDefinitions.add((DataFieldDefinition) dataFieldDefinition);
		}
	}

	public void addField(DataFieldDefinition fieldDefinition) {
		if (fieldDefinition.getName() == null || fieldDefinition.getName().trim().length() == 0) {
			fieldDefinition.setName(DEFAULT_COLNAME + (fieldDefinitions.size() + 1));
		}

		this.fieldDefinitions.add(fieldDefinition);
	}

	public void insertField(int index, DataFieldDefinition fieldDefinition) {
		if (fieldDefinition.getName() == null || fieldDefinition.getName().length() == 0) {
			fieldDefinition.setName(DEFAULT_COLNAME + (fieldDefinitions.size() + 1));
		}

		this.fieldDefinitions.add(index, fieldDefinition);
	}

	public DataFieldDefinition removeField(int index) {
		return fieldDefinitions.remove(index);
	}

	public DataFieldDefinition removeField(String name) {
		for (int j = 0; j < fieldDefinitions.size(); j++) {
			DataFieldDefinition def = fieldDefinitions.get(j);
			if (def.getName().equalsIgnoreCase(name)) {
				return fieldDefinitions.remove(j);
			}
		}

		return null;
	}

	public void removeAll() {
		fieldDefinitions.clear();
	}

	public int getFieldCount() {
		return fieldDefinitions.size();
	}

	public List<DataFieldDefinition> getFields() {
		return fieldDefinitions;
	}

	public String[] getFieldNames() {
		String[] values = new String[fieldDefinitions.size()];
		for (int i = 0; i < values.length; i++) {
			values[i] = fieldDefinitions.get(i).getName();
		}
		return values;
	}

	public DataRecord newDataRecordInstance() {
		DataRecord dr = new DataRecordImpl(this);
		return dr;
	}

	public DataRecordParser getParser() {
		return parser;
	}

	public void setParser(DataRecordParser parser) {
		this.parser = parser;
	}

	public DataRecordRender getRender() {
		return render;
	}

	public void setRender(DataRecordRender Render) {
		this.render = Render;
	}

	public void parseTitle(Object value) throws DataException {
		parser.detectColumn(this, value, true);
	}

	public DataRecord parse(Object value) throws DataException {
		return parser.parse(this, value);
	}

	public String rending(DataRecord value) {
		return render.rending(value);
	}

	public void validate(DataRecord value) {
		// TODO Auto-generated method stub

	}

}
